// app.js
App({
  onLaunch() {
  
  },
  isLogin(){
    return !!wx.getStorageSync('token')
  },
  getToken(){
    return wx.getStorageSync('token')
  },
  baseURL: 'https://api.it120.cc/conner',
  request(url, config = {}){
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: '加载中...'
      })
      const token = wx.getStorageSync('token')
      wx.request({
        url: this.baseURL + url,
        method: config.method || 'GET',
        data: config.data || '',
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: token||''
        },
        success: res => {
          if(res.data.code === 401 || res.data.code === 403){
            wx.showToast({
              title: '登录状态过期',
              icon:'error',
              success: res => {
                wx.removeStorageSync('token')
                wx.removeStorageSync('userInfo')
                wx.removeStorageSync('openId')
              }
            })
          }
          resolve(res)
        },
        fail: err => {
          reject(err)
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  globalData: {
    userInfo: null
  }
})
